﻿using E_Surfing.WP8Framework;
using Game77SharedLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;

namespace E_Surfing.SFReaderWP8.Model.Http
{
    /// <summary>
    /// 图片处理类
    /// </summary>
    public class ReaderImage
    {
        public delegate void ResponseImageEvent(BitmapImage bitmapImage, string contentId);
        public static BitmapImage defaultImage = null;

        public static string GetImageAddressId(string address)
        {
            return MD5.GetMd5String(address);
        }
        

        /// <summary>
        /// 网络获取图片  ---异步---
        /// </summary>
        /// <param name="imageUrl"></param>
        /// <param name="contentId"></param>
        public static void GetImageFromNet(ResponseImageEvent imageEvent, string imageUrl, string contentId, bool useDefaultBitmap = true)
        {
            if (!string.IsNullOrEmpty(contentId) && null != imageEvent)
            {
                if (ImageIsExist(contentId))
                {
                    imageEvent(GetImageFromLocal(contentId), contentId);
                }
                else if (!string.IsNullOrEmpty(imageUrl))
                {
                    ImageThreadPool imageThreadPool = new ImageThreadPool();
                    imageThreadPool.DownloadImageToThreadPool(imageEvent, imageUrl, contentId, useDefaultBitmap);
                }
            }
            else
            {
                if (null != imageEvent)
                {
                    imageEvent(null, contentId);
                }
            }
        }

        /// <summary>
        /// 判断图片在本地是否存在
        /// </summary>
        /// <param name="contentId"></param>
        /// <returns></returns>
        public static bool ImageIsExist(string contentId)
        {
            string imName = contentId + GlobalUnit.IMAGE_JPG;
            return WPLocalImage.WPImageIsExist(GlobalUnit.IMAGE_FILE, imName);
        }

        /// <summary>
        /// 读取本地图片 ---同步----
        /// </summary>
        /// <param name="contentId"></param>
        /// <returns></returns>
        public static BitmapImage GetImageFromLocal(string contentId)
        {
            BitmapImage bitmap = null;
            string imName = contentId + ".jpg";
            bitmap = WPLocalImage.WPGetImageFromLocal(GlobalUnit.IMAGE_FILE, imName);

            if (null == bitmap)
            {
                bitmap = GlobalUnit.sDefaultBitmap;
            }
            return bitmap;
        }

        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="imageBytes"></param>
        /// <param name="imageFileName"></param>
        public static bool SaveImage(byte[] imageBytes, string contentId)
        {
            string imName = contentId + GlobalUnit.IMAGE_JPG;
            return WPLocalImage.WPSaveImageToLocal(GlobalUnit.IMAGE_FILE, imName, imageBytes, true);
        }
    }
}
